package com.yxw.live_vod_boot.controller;

import com.yxw.live_vod_boot.dto.AddVodPlayDto;
import com.yxw.live_vod_boot.dto.OndemandVideoQueryDTO;
import com.yxw.live_vod_boot.dto.UpdateVideoDTO;
import com.yxw.live_vod_boot.dto.UploadVideoDTO;
import com.yxw.live_vod_boot.service.OnDemandVideoService;
import com.yxw.live_vod_boot.vo.OnDemandPlayVo;
import com.yxw.live_vod_boot.vo.OnDemandVideoVo;
import com.yxw.live_vod_boot.vo.VideoPalyStatisticsVo;
import com.yxw.live_vod_facade.dto.PageVodInfoListDto;
import com.yxw.live_vod_facade.dto.VideoDeleteDTO;
import com.yxw.live_vod_facade.vo.PageVodInfoListVO;
import com.yxw.live_vod_facade.vo.RecommendVodListVo;
import com.yxw.live_vod_facade.vo.VideoReviewVO;
import com.yxw.live_vod_facade.vo.VodSalesRevenueVo;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.helper.ResultHelper;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Set;

/**
 * 点播视频管理
 *
 * @author liuxin
 * @date 2023.11.15
 */
@CrossOrigin
@RestController
@Api(value = "点播视频管理", tags = "点播视频管理")
@Slf4j
@RequestMapping("/api/onDemand/video")
public class OnDemandVideoController {

    @Autowired
    private OnDemandVideoService onDemandVideoService;

    @PostMapping(value = "/uploadVideo")
    @ApiOperation(value = "视频上传")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID", required = true),
            @ApiImplicitParam(name = "titel", value = "视频标题", required = false),
            @ApiImplicitParam(name = "publicFlag", value = "是否公开 0-否 1：是", required = true),
            @ApiImplicitParam(name = "coverUrl", value = "封面路径", required = true),
            @ApiImplicitParam(name = "productId", value = "带货商品ID", required = false)
    })
    public BaseResult<OnDemandVideoVo> uploadVideo(
            @RequestParam(name = "videoFile")  MultipartFile videoFile,
            @RequestParam(name = "shopId") String shopId,
            @RequestParam(name = "titel") String titel,
            @RequestParam(name = "publicFlag") Integer publicFlag,
            @RequestParam(name = "coverUrl") String coverUrl,
            @RequestParam Long productId
    ) {
        UploadVideoDTO dto = new UploadVideoDTO();
        dto.setShopId(shopId);
        dto.setPublicFlag(publicFlag);
        dto.setTitel(titel);
        dto.setCoverUrl(coverUrl);
        dto.setProductId(productId);
        VideoReviewVO vo = onDemandVideoService.uploadVideo(videoFile,dto);
        return BaseResult.doSuccess(vo, "上传成功,请等待审核结果");
    }

    @PostMapping(value = "/uploadImages")
    @ApiOperation(value = "图片文件上传")
    public BaseResult<String> uploadImages(@RequestParam(name = "file") MultipartFile file){
        String url=onDemandVideoService.uploadImages(file);
        return BaseResult.doSuccess(url,"上传成功");
    }

    @PostMapping(value = "/pageVideo")
    @ApiOperation(value = "店铺点播视频列表")
    public BaseResult<PageVO<OnDemandVideoVo>> pageVideo(@Validated @RequestBody OndemandVideoQueryDTO dto) {
        PageVO<OnDemandVideoVo> voList = onDemandVideoService.pageVideo(dto);
        return ResultHelper.fillWithSuccess(voList);
    }

    @PostMapping(value = "/deleteVideoByIds")
    @ApiOperation(value = "批量删除视频")
    public BaseResult deleteVideoByIds(@Validated @RequestBody VideoDeleteDTO dto) {
        onDemandVideoService.deleteVideoByIds(dto);
        return BaseResult.doSuccess(null, "删除成功");
    }

    @PostMapping(value = "/getVideoPlayStatistics")
    @ApiOperation(value = "点播作品统计")
    public BaseResult<VideoPalyStatisticsVo> getVideoPlayStatistics(@RequestParam Long videoId) {
        VideoPalyStatisticsVo vo = onDemandVideoService.getVideoPlayStatistics(videoId);
        return BaseResult.doSuccess(vo, "查询成功");
    }

    @PostMapping(value = "/updateVideoById")
    @ApiOperation(value = "视频修改")
    public BaseResult updateVideoById(@RequestBody UpdateVideoDTO dto) {
        onDemandVideoService.updateVideoById(dto);
        return BaseResult.doSuccess(null, "修改成功");
    }

    @ApiOperation(value = "店铺点播作品列表详情查询")
    @PostMapping(value = "/pageVodInfoList")
    BaseResult<PageVO<PageVodInfoListVO>> pageVodInfoList(@RequestBody PageVodInfoListDto dto) {
        PageVO<PageVodInfoListVO> voList = onDemandVideoService.pageVodInfoList(dto);
        return ResultHelper.fillWithSuccess(voList);
    }

    @ApiOperation(value = "URL上传火山引擎后回调接口")
    @PostMapping(value = "/fileUploadByUrlComplete")
    public void fileUploadByUrlComplete(String json){
        onDemandVideoService.fileUploadByUrlComplete(json);
    }

    @ApiOperation(value = "点播视频播放记录")
    @PostMapping(value = "/saveVodPlay")
    public BaseResult saveVodPlay(@RequestBody @Validated AddVodPlayDto dto){
        OnDemandPlayVo vo= onDemandVideoService.saveVodPlay(dto);
        return BaseResult.doSuccess(vo,"新增成功");
    }

    @ApiOperation(value = "点播视频推荐列表查询")
    @PostMapping(value = "/pageVodRecommendList")
    public BaseResult<Set<RecommendVodListVo>> pageVodRecommendList(){
        Set<RecommendVodListVo> voList= onDemandVideoService.pageVodRecommendList();
        return BaseResult.doSuccess(voList,"查询成功");
    }

    @ApiOperation(value = "抽取热门数据和最新数据放入缓存(每天凌晨执行一次)")
    @PostMapping(value = "/extractVodByDay")
    public BaseResult extractVodByDay(){
        onDemandVideoService.extractVodByDay();
        return BaseResult.doSuccess(null,"执行成功");
    }
    @ApiOperation(value = "查询点播上架商品和订单统计信息")
    @PostMapping(value = "/getVodProductOrderByVideoId")
    public BaseResult<VodSalesRevenueVo> getVodProductOrderByVideoId(@RequestBody Long videoId){
        VodSalesRevenueVo vo=onDemandVideoService.getVodProductOrderByVideoId(videoId);
        return BaseResult.doSuccess(vo,"查询成功");
    }
}
